package br.festasconquista;

import java.net.URI;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class FragCadastro extends Fragment {
	EditText et_nome, et_preco, et_data, et_hora, et_local;
	Button bt_carregar_foto;
	private ImageView iv_baner;

	ImageView img, img1;
	int column_index;
	Intent intent = null;
	// Declare our Views, so we can access them later
	String logo, imagePath, Logo;
	Cursor cursor;
	String selectedImagePath;
	// ADDED
	String filemanagerstring;

	// constante que faz parte da intent que busca IMAGEM do SD
	public static final int IMAGEM_INTERNA = 1;

	@Override
	public View onCreateView(final LayoutInflater inflater,ViewGroup container, Bundle saveInstanceState) {
		View view = inflater.inflate(R.layout.cadastro, null);
		iv_baner = (ImageView) view.findViewById(R.id.imageView1);
		et_nome = (EditText) view.findViewById(R.id.et_cadastro_nome);
		et_nome.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					v.setBackgroundResource(R.drawable.et_focus_border_style);
				} else {
					v.setBackgroundResource(R.drawable.et_lost_focus_style);
				}

			}
		});

		et_local = (EditText) view.findViewById(R.id.et_cadastro_local);
		et_local.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					v.setBackgroundResource(R.drawable.et_focus_border_style);
				} else {
					v.setBackgroundResource(R.drawable.et_lost_focus_style);
				}

			}
		});

		et_preco = (EditText) view.findViewById(R.id.et_cadastro_preco);
		et_preco.addTextChangedListener(Mask.insert("R$###", et_preco));
		et_preco.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					v.setBackgroundResource(R.drawable.et_focus_border_style);
				} else {
					v.setBackgroundResource(R.drawable.et_lost_focus_style);
				}

			}
		});

		et_data = (EditText) view.findViewById(R.id.et_cadastro_data);
		et_data.addTextChangedListener(Mask.insert("##/##/####", et_data));
		et_data.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					v.setBackgroundResource(R.drawable.et_focus_border_style);
				} else {
					v.setBackgroundResource(R.drawable.et_lost_focus_style);
				}

			}
		});

		et_hora = (EditText) view.findViewById(R.id.et_cadastro_hora);
		et_hora.addTextChangedListener(Mask.insert("##:##", et_hora));
		et_hora.setOnFocusChangeListener(new OnFocusChangeListener() {

			@Override
			public void onFocusChange(View v, boolean hasFocus) {
				if (hasFocus) {
					v.setBackgroundResource(R.drawable.et_focus_border_style);
				} else {
					v.setBackgroundResource(R.drawable.et_lost_focus_style);
				}

			}
		});

		bt_carregar_foto = (Button) view
				.findViewById(R.id.bt_carregarFoto_cadastro);
		bt_carregar_foto.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent it = new Intent();
				it.setType("image/*");
				it.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(it.createChooser(it, "Selecione a figura"),IMAGEM_INTERNA);
			}
		});

		return view;
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == IMAGEM_INTERNA) {
			if (resultCode == getActivity().RESULT_OK) {
				Uri imagemselecionada = data.getData();

				String[] colunas = { MediaStore.Images.Media.DATA }; // _data

				Cursor cursor = getActivity().getContentResolver().query(imagemselecionada, colunas, null, null, null);
				if(cursor == null)
				cursor.moveToFirst();

				int indexColuna = cursor.getColumnIndex(colunas[0]);
				String pathImg = cursor.getString(indexColuna);
				cursor.close();

				Bitmap bitmap = BitmapFactory.decodeFile(pathImg);
				iv_baner.setImageBitmap(bitmap);
			}
		}
	}

}
